/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controlenotas;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jeferson
 */
public class Pdf {
    
    private File file;
    private FileOutputStream fos;
    private Rectangle pageSize = PageSize.A4;
    private float leftMagin = 15;
    private float rigthMargin = 15;
    private float topMargin = 15;
    private float bottonMargin = 15;
    private PdfWriter writer;
    private String docTitle;
    private Document doc = new Document(this.pageSize, leftMagin, rigthMargin, topMargin, bottonMargin);
    private Font fonte = new Font(Font.FontFamily.TIMES_ROMAN, 10, Font.NORMAL);
    private ArrayList<Paragraph> paragrafos = new ArrayList<Paragraph>();
    private ArrayList<PdfPTable> tabelas = new ArrayList<PdfPTable>();
    private PdfPTable currentTable;
    private int numColumns = 0;

    public Pdf(File file) {
        try {
            this.file = file;
            fos = new FileOutputStream(file);
            writer = PdfWriter.getInstance(doc, fos);
            doc.open();
            doc.addTitle("teste");
        } catch (DocumentException ex) {
            Logger.getLogger(Pdf.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Pdf.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void createCabecalho(String escola, String materia, String professor, String turma){
        addParagrafo(escola, Element.ALIGN_CENTER);
        addParagrafo("Resultado Parcial 2º Semestre", Element.ALIGN_CENTER);
        addParagrafo(materia + ": "+professor, Element.ALIGN_CENTER);
        //addParagrafo(professor, Element.ALIGN_CENTER);
        addParagrafo(turma, Element.ALIGN_CENTER);
        addParagrafo(" ", Element.ALIGN_CENTER);
    }
    
    public void createRodape(){
        addParagrafo("M1S: Média 1º Sem.   M2: Média 2º Sem.   MF: Média Final   "
                + "RS: Recuperação Semestral   SIT: Situação(ap=aprovado, rp=reprovado)", Element.ALIGN_CENTER);
    }

    public boolean addPage(){
        return doc.newPage();
    }

    public void addParagrafo(String texto, int alinhamento) {
        try {
            Chunk c = new Chunk(texto, fonte);
            Paragraph parag = new Paragraph(c);
            paragrafos.add(parag);
            parag.setAlignment(alinhamento);
            doc.add(parag);
        } catch (DocumentException ex) {
            Logger.getLogger(Pdf.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void createTable(int columns, int[] widths) {
        try {
            this.numColumns = columns;
            PdfPTable table = new PdfPTable(columns);
            tabelas.add(table);
            table.setWidthPercentage(100f);
            table.setWidths(widths);
            currentTable = table;
        } catch (DocumentException ex) {
            Logger.getLogger(Pdf.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addCelula(String texto, int vAlign, int hAlign, BaseColor bgColor, int border){
        if (currentTable == null) {
            return;
        }
        Phrase p = new Phrase(new Chunk(texto, fonte));
        PdfPCell cell = new PdfPCell(p);
        cell.setHorizontalAlignment(hAlign);
        cell.setVerticalAlignment(vAlign);
        cell.setBackgroundColor(bgColor);
        cell.setBorder(border);
        currentTable.addCell(cell);
    }

    public void addTableRow(String[] textos, int[] vAlign, int[] hAlign, BaseColor[] bgColor, int[] border, Font[] font ){
        for(int i=0; i<numColumns; i++){
            this.fonte = font[i];
            addCelula(textos[i], vAlign[i], hAlign[i], bgColor[i], border[i]);
        }
    }

    public void setColumnsWidths(int[] widths){
        try {
            currentTable.setWidths(widths);
        } catch (DocumentException ex) {
            Logger.getLogger(Pdf.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void addTale(){
        try {
            doc.add(currentTable);
        } catch (DocumentException ex) {
            Logger.getLogger(Pdf.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void close(){
        doc.close();

    }

    public void setFontStyle(int style){
        this.fonte.setStyle(style);
    }

    public void setFontSize(float size){
        this.fonte.setSize(size);
    }


}
